package com.adb.indian.rto.utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.net.ConnectivityManager;

public class Utility {
	
	private static String TAG = Utility.class.getCanonicalName();
	
	public static void SystemUpgrade(){
		Log.debug(TAG,"[ Utility ]");
		int intLevel = Integer.parseInt(Pref.getValue(Const.PREF_SYSTEM_LEVEL,Const.DVALUS_SYSTEM_LEVEL));
		switch (intLevel) {
		case 0:
			new DBHelper().upgrade(intLevel);
			intLevel++;
			
//		case 1:			
			//IF NEED TO UPGRADE DB LEVEL OPEN COMMENT 
//			upgradeLevel1();
//			intLevel++;
			 
			
		}
		Pref.setValue(Const.PREF_SYSTEM_LEVEL, intLevel + "");
	}
	
	public static boolean isOnline() {
	
		try {
			ConnectivityManager cm = (ConnectivityManager) Const.CONTEXT
					.getSystemService(Context.CONNECTIVITY_SERVICE);
	
			if (cm != null) {
				return cm.getActiveNetworkInfo().isConnectedOrConnecting();
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
	
	public static String readStream(InputStream is) throws IOException {
		int ch = 0;
		String str = new String();

		while ((ch = is.read()) != -1) {
			str += (char) ch;
		}

		is.close();

		return str;
	}

	public static void veirfyLogPath() throws IOException {
          File dir = new File(Const.LOG_DIR);

          if (!dir.exists()) {
                  dir.mkdirs();
          }

          dir = null;
	  }
	
	public static File verifyLogFile() throws IOException {
          File logFile = new File(Const.LOG_DIR + "/Log_"
                          + new SimpleDateFormat("yyyy_MM_dd").format(new Date())
                          + ".html");
          FileOutputStream fos = null;

          Utility.veirfyLogPath();

          if (!logFile.exists()) {
                  logFile.createNewFile();
                  
                  fos = new FileOutputStream(logFile);
                  
                  String str = "<TABLE style=\"width:100%;border=1px\" cellpadding=2 cellspacing=2 border=1><TR>"
                          + "<TD style=\"width:30%\"><B>Date n Time</B></TD>"
                          + "<TD style=\"width:20%\"><B>Title</B></TD>" 
                          + "<TD style=\"width:50%\"><B>Description</B></TD></TR>";
                  
                  fos.write(str.getBytes());
          }

          if (fos != null){
                  fos.close();
          }
          
          fos = null;
          
          return logFile;
  }
	
	public static void verifyAppHome(){
		
	     File dir = new File(Const.APP_HOME);

         if (!dir.exists()) {
                 dir.mkdirs();
         }

         dir = null;
	}
	
	public static boolean removeFileIfExist(String fileName){
		
		  File dir = new File(fileName);
		  if (!dir.exists()) {
			  return true;
          }else{
        	  return dir.delete();
          }
	}
	
	
}
